<cfcomponent displayname="Library Controller" extends="ModelGlue.unity.controller.Controller" output="false">
	<!--- 
		Any function set up to listen for the onRequestStart message happens before any of the <event-handlers>.
		This is a good place to put things like session defaults.
	--->
	<cffunction name="init" access="public" returntype="libcontroller" output="false">
		<cfargument name="ModelGlue" required="true" type="ModelGlue.ModelGlue" />
		<cfargument name="instanceName" required="true" type="string" />
		<cfset var modelPath = "snipex.model" />
		<cfset var facadePath = "snipex.facades" />
		
		<cfset super.Init(arguments.ModelGlue) />
		<cfset variables.userFacade = createobject("component", facadePath & ".userFacade") />
		<cfset variables.libService = getModelGlue().getConfigBean("libService") />
		<cfset variables.libAccessService = getModelGlue().getConfigBean("libAccessService") />
		<cfset variables.reactor = getModelGlue().getConfigBean("ormService") />
		<cfset variables.udfs = createobject("component", modelPath & ".udfs") />

		<cfreturn this>		
	</cffunction>
		
	<cffunction name="getLib" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfif isNumeric(arguments.event.getValue('libID'))>
			<cfset arguments.event.setValue("lib", 	variables.libService.getByID(arguments.event.getValue("libID"))) />	
		<cfelse>
			<cfset arguments.event.setValue("lib", 	variables.libService.getByID(0)) />	
		</cfif>
		
	</cffunction>
	
	<cffunction name="getLibs" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		
		<cfif arguments.event.valueExists('userID')>
			<cfif arguments.event.getValue('userID') is not variables.userFacade.getUserID() 
					and (listfindnocase(arguments.event.getValue('roles'),'sysadmin')  or listfindnocase(arguments.event.getValue('roles'),'admin') )>
				<cfset arguments.event.setValue("libs", 
								   variables.libService.getByUserID(arguments.event.getValue('userID'))) />			
			<cfelse>
				<cfset arguments.event.setValue("libs", 
								   variables.libService.getByUserID(variables.userFacade.getUserID())) />
			</cfif>
		<cfelse>
			<cfset arguments.event.setValue("libs", 
								   variables.libService.getByUserID(variables.userFacade.getUserID())) />
		</cfif>

	</cffunction>
	
	<cffunction name="saveLib" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfset var libBean = false />
		<cfif not isNumeric(arguments.event.getValue('libID')) >
			<cfset libBean = variables.libService.getByID(0) />
		<cfelse>
			<cfset libBean = variables.libService.getByID(arguments.event.getValue('libID')) />
		</cfif>
		<cfif libBean.getLibID() is 0>
			<cfset libBean.setcreated(now()) />
		</cfif>		
		<cfif arguments.event.ValueExists('name')>
			<cfset libBean.setName(arguments.event.getValue("name")) />
		</cfif>		
		<cfif arguments.event.ValueExists('description')>
			<cfset libBean.setDescription(arguments.event.getValue("description")) />
		</cfif>		
		<cfif arguments.event.ValueExists('isPublic')>
			<cfset libBean.setIsPublic(arguments.event.getValue("isPublic")) />
		</cfif>		
		<cfif arguments.event.ValueExists('parent')>
			<cfset libBean.setParent(arguments.event.getValue("parent")) />
		</cfif>
		<cfset libBean.setUpdated(now()) />

		<cfif arguments.event.valueExists('userID')>
			<cfif arguments.event.getValue('userID') is not variables.userFacade.getUserID() 
					and (listfindnocase(arguments.event.getValue('roles'),'sysadmin')  or listfindnocase(arguments.event.getValue('roles'),'admin') )>
				<cfset libBean.setOwnerID(arguments.event.getValue('userID')) />
			<cfelse>
				<cfset libBean.setOwnerID(variables.userFacade.getUserID()) />
			</cfif>
		<cfelse>
			<cfset libBean.setOwnerID(variables.userFacade.getUserID()) />
		</cfif>
		
		<cfset libBean = variables.libService.save(libBean) />
		<cfset arguments.event.setValue("libID", libBean.getLibID()) />
		
	</cffunction>
	
	<cffunction name="saveLibAccess" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfset var libAccessBean = false />
		<cfset var userID = false />
		
		<cfif arguments.event.valueExists('userID')>
			<cfif arguments.event.getValue('userID') is not variables.userFacade.getUserID() 
					and (listfindnocase(arguments.event.getValue('roles'),'sysadmin')  or listfindnocase(arguments.event.getValue('roles'),'admin') )>
				<cfset libAccessBean = variables.libAccessService.getByID(arguments.event.getValue('userID'),arguments.event.getValue('libID')) />
				<cfset userID = arguments.event.getValue('userID') />
			<cfelse>
				<cfset libAccessBean = variables.libAccessService.getByID(variables.userFacade.getUserID(),arguments.event.getValue('libID')) />
				<cfset userID = variables.userFacade.getUserID() />
			</cfif>
		<cfelse>
			<cfset libAccessBean = variables.libAccessService.getByID(variables.userFacade.getUserID(),arguments.event.getValue('libID')) />
			<cfset userID = variables.userFacade.getUserID() />
		</cfif>
		<cfsavecontent variable="t">
			<cfoutput>#libAccessBean.hasid() #</cfoutput>
		<cfdump var="#libAccessBean.getInstanceData()#">
		</cfsavecontent>
		<cffile action="append" addnewline="true" file="#expandpath('test.cfm')#" output="#t#">
		<cfif not libAccessBean.hasid()  or (libAccessBean.hasid() and libAccessBean.getuserid() is '')>
			<cfset libAccessBean.setUserID(userID) />
			<cfset libAccessBean.setLibID(arguments.event.getValue('libID')) />
			<cfset variables.libAccessService.save(libAccessBean) />
		</cfif>
	</cffunction>
	
	<cffunction name="deleteLib" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		
	</cffunction>
	
	<cffunction name="deleteLibAccess" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		
	</cffunction>
	
	<cffunction name="shareLib" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		
	</cffunction>
	
	<cffunction name="checkLibIDAccess" access="public" returntype="void" output="false">
		<cfargument name="event" type="any"/>
		<cfset var qlib = false />
		
		<cfif arguments.event.valueExists('libID')>
			<cfif arguments.event.getValue('libID') gt 0>
				<cfset qlib = variables.libAccessService.getCheckAccess(libid=arguments.event.getValue('libID'),userid=variables.userFacade.getUserID())/>
				<cfif qlib.recordCount is 0 and (listfindnocase(arguments.event.getValue('roles'),'sysadmin') is 0 and  listfindnocase(arguments.event.getValue('roles'),'admin') is 0)>
					<cfset arguments.event.addResult('noaccess')>
				</cfif>
			</cfif>
		</cfif>
		
	</cffunction>
</cfcomponent>